import { LogHelper } from "../../utils/logHelper";

export class AttackFlagTask implements IFlagTask{
  run(flag: Flag,param:string | undefined): void {
    if(param){
      const room = Game.rooms[param];
      if(room){
        let role = "attack_flag";
        // 判断是否存在攻击角色
        const spawn = room.spawn();
        const creeps = Object.values(Memory.creeps).filter((c)=> c.role === role && c.flagNameTarget === flag.name);
        const queue = room.spawnQueueGetRole(role,(c)=> c.memory.flagNameTarget === flag.name);

        const count = 5;
        const sum = creeps.length + queue.length;

        if(spawn){
          if(sum < count && !spawn.spawning){
            const body = room.calculateCreepBody(
              {
                [TOUGH]:0.01,
                [MOVE]:0.2,
                [HEAL]:0.39,
                [ATTACK]:0.4
              }
            );
            for (let index = 0; index < (count - sum); index++) {
              room.spawnQueueAdd(role,body,{role:role,bornRoom:room.name,flagNameTarget:flag.name});
              LogHelper.log(`Flag[${flag.name}] 申请孵化攻击者`);
            }
          }
        }
      }
      else {
        LogHelper.warn(`Flag ${flag.name} 请传入正确房间名 "攻击:房间名"`);
        flag.remove();
      }
    }
    else {
      LogHelper.warn(`Flag ${flag.name} 请传入正确参数 "攻击:房间名"`);
      flag.remove();
    }
  }
}